package com.bq.server.enumeration;

import com.baomidou.mybatisplus.annotation.EnumValue;

import java.util.Objects;


/**
 * 是和否
 * @author: ChenSH
 * @date: Create by in 15:21 2019-03-04
 */
public enum YesOrNoEnum implements Enumeration {

    YES(1, "是") {
        @Override
        public boolean toBoolean() {
            return true;
        }
    },

    NO(0, "否") {
        @Override
        public boolean toBoolean() {
            return false;
        }
    };

    @EnumValue
    private final Integer code;

    private final String describe;

    YesOrNoEnum(Integer code, String describe) {
        this.code = code;
        this.describe = describe;
    }

    @Override
    public Integer value() {
        return code;
    }

    @Override
    public String describe() {
        return describe;
    }

    public abstract boolean toBoolean();

    public static YesOrNoEnum valueOf(boolean value) {
        return value ? YesOrNoEnum.YES : YesOrNoEnum.NO;
    }

    public static YesOrNoEnum valueOf(Integer value) {
        return valueOf(value, null);
    }

    public static YesOrNoEnum valueOf(Integer value, YesOrNoEnum defaultValue) {
        if (Objects.equals(value, 0)) {
            return YesOrNoEnum.NO;
        } else if (Objects.equals(value, 1)) {
            return YesOrNoEnum.YES;
        } else {
            return defaultValue;
        }
    }
}
